﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class register : System.Web.UI.Page
{
    DataClassesDataContext db = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {

        if ((db.proc_ISOpenDate(1)) < 1)
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>alert('该功能暂未开放，请等待！');window.location='../login.aspx'</script>");
            return;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        var result = (from p in db.proc_GetStuInfobyPwd(TextBox1 .Text )
                      select p).ToList();
        var BkYearRes = (from p in db.proc_GetBkgxNowYear()
                         select p).ToList().First();
        int flag = db.proc_CheckSidfy(TextBox5.Text.ToString().Trim());
        if (result.Count() > 0)
        {
            if (result.First().bkyear == BkYearRes.dYear)
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>window.alert('该手机号已被注册，请使用找回密码功能！');</script>");
                return;
            }
        }
        if (flag == 1)
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>window.alert('该身份证号已被注册，请使用找回密码功能！');</script>");
            return;
        }
        if (TextBox2.Text.ToString().Length < 4)
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>window.alert('密码太短，为确保账户安全请重新设置！');</script>");
            return;
        }
        int ansOneNo = DropDownList1.SelectedIndex;
        int ansTwoNo = DropDownList2.SelectedIndex;
        if (TextBox2.Text != TextBox3.Text)
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>window.alert('两次密码输入不一致！');</script>");
            return;
        }
        if (CheckBox1.Checked == false)
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>window.alert('请勾选同意条款！');</script>");
            return;
        } 
        try
        {
            if (db.proc_Register(TextBox4.Text.Trim(), TextBox2.Text.Trim(), TextBox5.Text.Trim(), TextBox1.Text.Trim(), TextBox6.Text.Trim(), ansOneNo, TextBox7.Text.Trim(), ansTwoNo, TextBox8.Text.Trim()) == -1)
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>window.alert('注册失败！');</script>");
                return;
            }
            else
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "succeed", "<script>window.alert('注册成功！');window.location='../login.aspx';</script>");
                db.SubmitChanges();
            }
        }
        catch
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>window.alert('注册失败,请重试！');</script>");
        }

    }
    
}